package avicit.bdp.dcs.job.service;

import avicit.bdp.common.utils.enums.JobType;
import avicit.platform6.core.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @金航数码科技有限责任公司
 * @作者：liyb
 * @邮箱：liyb@avic-digital.com
 * @创建时间： 2020-09-11 13:36
 * @类说明：
 * @修改记录：
 */
@Service
public class JobFactory {

    private static Map<Integer, JobInterface> jobMap;

    @Autowired
    private StructureJobService structureJobService;
    @Autowired
    private StreamJobService streamJobService;
    @Autowired
    private ApiJobService apiJobService;
    @Autowired
    private FileJobService fileJobService;

    private void init() {
        jobMap = new HashMap<>(16);
        jobMap.put(JobType.TRANSFER.getCode(), structureJobService);
        jobMap.put(JobType.SYNC.getCode(), structureJobService);
        jobMap.put(JobType.SEMISTRUCTURE.getCode(), fileJobService);
        jobMap.put(JobType.STREAM.getCode(), streamJobService);
        jobMap.put(JobType.API_HTTP.getCode(), apiJobService);
    }

    public JobInterface getJobInterface(Integer jobType) {
        if (jobMap == null) {
            init();
        }

        if (jobType == null || !jobMap.containsKey(jobType)) {
            throw new BusinessException("未知的任务类型或没有对应处理任务的处理器");
        }

        return jobMap.get(jobType);
    }

}
